﻿
//Component, the component's base class.
LightScript.using("LightScript.Core", function()
{
    LightScript.Core.createNamespace("LightScript.Components");
    var thisClass = LightScript.Core.createClass("LightScript.Components.Component", function(canBeFound)
    {
        this.id = LightScript.Core.newId();
        this.canBeFound = !!canBeFound;
        if(this.canBeFound)
            LightScript.ObjectList.add(this.id, this);
    });

    //# Private Members

    //# Constructions

    //# Public Members
    thisClass.prototype.id = null;
    thisClass.prototype.canBeFound = null;

    //# Private Methods

    //# Public Methods
    thisClass.prototype.getObjectId = function(id)
    {
        return id == this.id ? id : this.id + "_" + id;
    }

    thisClass.prototype.dispose = function()
    {
        LightScript.cache.clear("evt_" + this.id + "_");  //Clear all the event handlers of this component.
        if(this.canBeFound)
            LightScript.ObjectList.remove(this.id);
    }

    // Find the component by id.
    thisClass.find = function(id)
    {
        return LightScript.ObjectList.get(id);
    }

    //# Private Event Handlers

    //# Public Event Handlers
    thisClass.prototype.add_propertyChange = function(handler)
    {
        LightScript.EventList.add(this.getObjectId(this.id), "propertyChange", handler);
    }

    thisClass.prototype.remove_propertyChange = function(handler)
    {
        LightScript.EventList.remove(this.getObjectId(this.id), "propertyChange", handler);
    }

    thisClass.prototype.raisePropertyChange = function(eventArgs)
    {
        LightScript.EventList.raise(this.getObjectId(this.id), "propertyChange", this, eventArgs);
    }

    //# Shortcuts
});
